//////////////////////////////////////////////////////////////
// This class provide a timer to do some works.
// Call StartTimer() to enable it and call StopTimer() to stop
// it. 
//////////////////////////////////////////////////////////////

#ifndef CTIMER_H_
#define CTIMER_H_

#include <sys/time.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <pthread.h>

typedef void (*TimerProc) (void *) ;

class CTimer ;

struct TimerEntry
{
    CTimer * pCTimerObject ;
    void   * pCallBackParm ;
} ;

class CTimer
{
private:
    //Set by user
    long            userFrequency ; //Frequency to be setted in RTC clock, in unit of Hz. Default to be 2 Hz.
    long            deltaTime ;     //when startTimer. In unit of second
    TimerProc       userCallBack ;  // when this object is created.
    void          * userParm ;      //parm of userCallBack.

    unsigned long   data ;
    int             rtcFD ;
    pthread_t       timerThreadID ;
    long            counter ;

    bool            isTimeout ( ) ;
    static void   * threadRoutineWrapper (void * pTimerEntry) ;
    void          * threadRoutine ( void * pCallBackParm );

public:
                    CTimer () ;
                    CTimer (TimerProc userProc, void * pParm, long userFrequency=2) ;
    virtual         ~CTimer() ;
    void            startTimer(int time) ; //In unit of second
    void            stopTimer() ;
};

#endif /* CTIMER_H_ */
